
package pl.genapp.stocksense.libraries;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.graphics.Shader;
import android.preference.PreferenceManager;
import android.util.AttributeSet;
import android.util.Log;

public class SentGraphCircles extends SentGraph {
	
	private static final int COLOR_EMPTY_CIRCLE = 0xFFCDCDCD;
	private static final int NUMBER_OF_CIRCLES = 7;
	private Paint circle = new Paint();
	
	public SentGraphCircles(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		circle = new Paint();
		circle.setAntiAlias(true);
		circle.setStyle(Paint.Style.FILL_AND_STROKE);
		circle.setColor(0xAACDCDCD);
		circle.setStrokeWidth(0);
		
		super.setLegendRendered(legendRendered);
		legendRendered.setDeltaAngle(30);
		
	}
	
	Shader getGradient(float x, float y, float x2, float y2, int id) {
		
		return new LinearGradient(x, y, x2, y2, SENTIMENTS_COLORS[id], COLOR_EMPTY_CIRCLE, Shader.TileMode.CLAMP);
		
	}
	
	void drawCirclesForSentiment(Canvas canvas, int sentNo, float sentValue) {
		
		int angle = 60 * sentNo + 30;
		
		// Ustawienie gradientu
		Point gradientEnd = getPointOnCircumference(getGraphCenter(), getGraphCenter(), angle, getGraphSize() * sentValue);
		Shader shader = getGradient(getGraphCenter(), getGraphCenter(), gradientEnd.x, gradientEnd.y, sentNo);
		circle.setShader(shader);
		
		for (int i = 0; i < NUMBER_OF_CIRCLES; i++) {
			
			int distance = (getGraphSize() / 2 - 2 * getRadius()) / NUMBER_OF_CIRCLES;
			
			Point point = getPointOnCircumference(getGraphCenter(), getGraphCenter(), angle, distance * i + 3 * getRadius());
			
			canvas.drawCircle(point.x, point.y, getRadius(), circle);
			
		}
		
	}
	
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		for (int i = 0; i < NUMBER_OF_SENTIMENTS; i++) {
			
			drawCirclesForSentiment(canvas, i, sentValues[i]);
		}
		
		if (getLegendRendered() != null) {
			getLegendRendered().drawLegend(canvas);
			
		}
		
	}
	
	int getRadius() {
		return (int) (getGraphSize() / 2 / (2 * NUMBER_OF_CIRCLES + 3));
	}
	
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);
		
	}
	
	private SentGraph.LegendRenderer legendRendered = new LegendRenderer(new int[] { 0, 0, 0, 0 }, new int[] { 30, 30 }, new int[] { 0, 10 }, this) {
		
		void drawIcon(Canvas canvas, RectF frame, int sentNo) {
			
			Paint circleLegend = new Paint();
			circleLegend = new Paint();
			circleLegend.setAntiAlias(true);
			circleLegend.setStyle(Paint.Style.FILL_AND_STROKE);
			circleLegend.setColor(SENTIMENTS_COLORS[sentNo]);
			
			canvas.drawCircle(frame.centerX(), frame.centerY(), frame.width() / 4, circleLegend);
		};
		
	};
	
	@Override
	void selectSentiment(int sentNo) {
		
		onSentClickListener.onSentClick(sentNo);
		
		postInvalidate();
		
	}
	
	@Override
	int getClickedSentNumber(float x, float y) {
		
		float delta_x = x - getGraphCenter();
		float delta_y = y - getGraphCenter();
		
		double theta_radians = Math.atan2(delta_y, delta_x);
		
		int angle = (int) (theta_radians * (180 / Math.PI));
		
		if (angle < 0) angle = 360 + angle;
		
		int id = (int) angle / 60;
		
		return id;
		
	}
	
}
